package org.common.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * String检测类
 */
public class StringChecker {

    // 账号校验的正则表达式
    private static final Pattern ACCOUNT_PATTERN = Pattern.compile("^[A-Za-z][A-Za-z0-9]{8,14}$");
    // 密码校验的正则表达式
    private static final Pattern PASSWORD_PATTERN = Pattern.compile("^[A-Za-z0-9]{9,15}$");

    /**
     * 账号密码合法检测
     */
    public static boolean checkAccountNameAndPassword(String accountName, String password) {
        return checkAccountName(accountName) && checkPassword(password);
    }
    /**
     * 账号合法性检测
     */
    private static boolean checkAccountName(String accountName) {
        if (accountName == null  || accountName.trim().isEmpty()) {
            return false;   // 账号名不能为空
        }
        accountName = accountName.trim();
        Matcher matcher = ACCOUNT_PATTERN.matcher(accountName);
        return matcher.matches();
    }
    /**
     * 密码合法性检测
     */
    private static boolean checkPassword(String password) {
        if (password == null || password.trim().isEmpty()) {
            return false; // 如果密码为空，则不合法
        }
        password = password.trim(); // 去除密码两端的空格
        Matcher matcher = PASSWORD_PATTERN.matcher(password);
        return matcher.matches();
    }

}
